<?php
session_start();
require_once("../Structure/fonctions.php");

$PAGE_TITLE = "Profil public - Lolgames";
$CONTENT_INCLUDE = "user.html";

if (isset($_GET["id"])) {
    $bdd = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_BASE);
	
	include("../Structure/updateUser.php");
	
    $article = false;


    //------------------------------------------------------------------//
    //      Informations                                                //
    //------------------------------------------------------------------//

    $query = "SELECT  statusID
              FROM    users
              WHERE   id = " . $_GET["id"] . " ;";
    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

    if (mysqli_num_rows($res) == 1) {
        $row = mysqli_fetch_array($res);
    }

    if (isset($_POST["no_article"])) {
        switch ($row["statusID"]) {
            case 10:
            case 12:
                if ($_POST["no_article"]) {
                    $status = 12;
                } else {
                    $status = 10;
                }
                break;

            case 11:
            case 13:
                if ($_POST["no_article"]) {
                    $status = 13;
                } else {
                    $status = 11;
                }
                break;

            case 19:
                $status = 19;
                break;
        }
    }

    if (isset($_POST["no_comment"])) {
        switch ($row["statusID"]) {
            case 10:
            case 11:
                if ($_POST["no_comment"]) {
                    $status = 11;
                } else {
                    $status = 10;
                }
                break;

            case 12:
            case 13:
                if ($_POST["no_comment"]) {
                    $status = 13;
                } else {
                    $status = 12;
                }
                break;

            case 19:
                $status = 19;
                break;
        }
    }

    if (isset($_POST["ban"])) {
        if ($_POST["ban"]) {
            $status = 19;
        } else {
            $status = 10;
        }

    }

    if (isset($status)) {
        $query = "UPDATE  users
                  SET     statusID = ".$status."
                  WHERE   id = " . $_GET["id"] . " ;";
        $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
    }

    // Modifie le grade
    if (isset($_POST["submit-role"])) {
        $query = "UPDATE  users
                  SET     roleID = ".$_POST['change-role']."
                  WHERE   id = ".$_GET["id"]." ;";
        $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
    }

        //  Modifie l'avatar depuis un lien internet
    if (isset($_POST["submit-image-url"])) {
        $avatar = $_POST["avatar"];

        if($imgId == 0)
        {
            $query = "INSERT INTO images (link, autorID, statusID)
                      VALUES      ('".$link."', ".$_GET["id"].", 1) ;";
            $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

            $query = "UPDATE  users
                      SET     avatarID = LAST_INSERT_ID()
                      WHERE   id = ".$_GET["id"]." ;";
            $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
        }
        else
        {
            $query = "UPDATE  images
                      SET     link = '".$link."', statusID = 1
                      WHERE   id = ".$imgId." ;";

            $res = mysqli_query($bdd, $query);
        }
    }

    //  Modifie l'avatar depuis un fichier local
    if (isset($_POST["submit-image-upload"]) && $_FILES["avatar"]["error"] == 0) {
        if ($_FILES["avatar"]["size"] < 5242880 /*5 Mio*/ ) {
            $extensions_valides = array( '.jpg' , '.jpeg' , '.gif' , '.png' , '.bmp' );
            $extension_upload = strtolower(strrchr($_FILES["avatar"]["name"], '.'));
            if (in_array($extension_upload,$extensions_valides)) {
                $move = move_uploaded_file($_FILES["avatar"]["tmp_name"], LOCALROOT."img/avatar/".$_GET["id"].$extension_upload);
                if ($move) {

                    $query = "SELECT avatarID
                              FROM users
							  WHERE id = ".$_GET["id"].";";

                    $res = mysqli_query($bdd, $query);

                    $row = mysqli_fetch_array($res);

                    $imgId = $row['avatarID'];
                    $link = "img/avatar/".$_GET["id"].$extension_upload;

                    if($imgId == 0)
                    {
                        $query = "INSERT INTO images (link, autorID, statusID)
							      VALUES      ('".$link."', ".$_GET["id"].", 1) ;";
                        $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

                        $query = "UPDATE  users
								  SET     avatarID = LAST_INSERT_ID()
								  WHERE   id = ".$_GET["id"]." ;";
                        $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
                    }
                    else
                    {
                        $query = "UPDATE  images
								  SET     link = '".$link."', statusID = 1
								  WHERE   id = ".$imgId." ;";

                        $res = mysqli_query($bdd, $query);
                    }
                }
            }
        }
    }

    $query = "SELECT  pseudo, lastName, firstName, email, link, u.roleID, u.statusID, hireDate, i.statusID imgStatus
              FROM    users u
              JOIN    images i ON   u.avatarID = i.id
              WHERE   u.id = " . $_GET["id"] . " ;";
    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

    if (mysqli_num_rows($res) == 1) {
        $row = mysqli_fetch_array($res);
        $PAGE_TITLE = "Profil public de ".$row['pseudo']." - Lolgames";

        if ($row["roleID"] >= 1) {
            $article = true;
        }
    }


    //------------------------------------------------------------------//
    //      Mes articles                                                //
    //------------------------------------------------------------------//
    $query = "SELECT  COUNT(*) nb, ROUND(AVG(note),1) moy
              FROM    articles
              WHERE   autorID = " . $_GET["id"] . "
                AND   statusID = 1 ;";
    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

    if (mysqli_num_rows($res) == 1) {
        $row_art_stat = mysqli_fetch_array($res);
    }

    $query = "SELECT    a.id, title, content, type, a.statusID, note, link, a.date, i.statusID imgStatus
              FROM      articles a
              JOIN      images i ON a.imageID = i.id
              WHERE     a.autorID = " . $_GET["id"] . "
                AND     a.statusID = 1
              ORDER BY  date DESC;";
    $res_art = mysqli_query($bdd, $query) or die(mysqli_error($bdd));


    //------------------------------------------------------------------//
    //      Mes commentaires                                            //
    //------------------------------------------------------------------//
    $query = "SELECT  COUNT(*) nb, ROUND(AVG(note),1) moy
              FROM    comments
              WHERE   autorID = " . $_GET["id"] . "
                AND   statusID = 1 ;";
    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

    if (mysqli_num_rows($res) == 1) {
        $row_com_stat = mysqli_fetch_array($res);
    }

    $query = "SELECT    a.id, title, c.content, c.statusID, c.note, c.date
              FROM      comments c
              JOIN      articles a ON c.articleID = a.id
              WHERE     c.autorID = " . $_GET["id"] . "
                AND     c.statusID = 1
              ORDER BY  c.date DESC;";
    $res_com = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

}

include_once("../Structure/structure.php");

?>